#include "HiBeamView.h"
#include "F3LampInterfaces.h"
#include "LampModel.h"

static void hibeamCanSignalFeedback()
{
    Set_CANSIG_LeHiBeamOnSt(LocalQuery_LeHiBeamOnSt());
}

float calculateHiBeamDerateCoefficient()
{
    float coefficient = 100.0f;

    do
    {
        if(IsLoBeamNTCOpenCircuit())
        {
            //Ignore
            break;
        }

        if(IsHiBeamNTCOpenCircuit() || IsHiBeamNTCOpenCircuit())
        {
            coefficient = LocalQuery_HibeamBrightness_Temperature();
        }

    } while (false);
    
    float coefficient_v = LocalQuery_HibeamBrightness_Voltage();
    coefficient = coefficient < coefficient_v ? coefficient : coefficient_v;

    return coefficient;
}

void execute_HiBeamView(HiBeamView* this_)
{
    hibeamCanSignalFeedback();
    Set_Lamp_Ctrl_HiBeamPwm(HibeamDefaultPWM * calculateHiBeamDerateCoefficient() / 100.0f);
    switch(this_->model_->onOffStatus_)
    {
        case OnOff_On:
            Set_HiBeam_OnOffState(true);
            break;
        
        case OnOff_Off:
            Set_HiBeam_OnOffState(false);
            break;
            
        case OnOff_Initial:
            break;
        
        default:
            //ignore
            break;
    }
}
